$(function(){
	findCommodity();
	findBranch();
});
function findCommodity(){
	$("#comSelect").html("");
	var url="../../commodity/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#comSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#comSelect").append("<option value="+item.comId+">"+item.comName+"</option>");
			load();
		});
	},"json");
}
function findBranch(){
	$("#braSelect").html("");
	var url="../../branch/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#braSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#braSelect").append("<option value="+item.braId+">"+item.braName+"</option>");
			load();
		});
	},"json");
}
function exit(){
	$("#maTypeName").val("");
	$("#operUser").val("");
	$("#typeId").val("");
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
  var table = layui.table
  ,laydate = layui.laydate
  ,form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit;
  
  //日期
  laydate.render({
    elem: '#date'
  });
  
  laydate.render({
	    elem: '#time'
  });
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    
    if(obj.event === 'edit'){
    	var url="../../matype/findById.action";
        var data={"typeId":da.typeId};
        $.post(url,data,function(mes){
        	layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '数据修改'
    	        ,area: ['500px', '260px']
        		,offset: 'auto'
    	        ,content:$('#addOrUpdate')
    	        ,success: function(layero){
    	        	$("#operUser").val(mes.oper);
    	        	$("#maTypeName").val(mes.typeName);
    	        	$("#typeId").val(mes.typeId);
    	        	//渲染
    	        	load();
        		},end : function() {
    	        	//弹框关闭的回调函数
    				$("#addOrUpdate").hide();
    				exit();
    			}
    	    });
        	
        },"json");
    }else if(obj.event === "detail"){
    	//查看
    	layer.open({
	        type: 1	 //此处以iframe举例
	        ,title: '查看详情'
	        ,area: ['700px', '400px']
    		,offset: 'auto'
	        ,content:$("#detailDiv")
	        ,success: function(layero){
	        	table.reload('InDetail', {
			        page: {
			          curr: 1 //重新从第 1 页开始
			        },where: {
			        	ciId:da.ciId,
			        }
			      });
	        },end : function() {
    			$("#detailDiv").css({"display":'none'});
			}
	    });
    }
    
  });
  var $ = layui.$, active = {
		    reload: function(){
		      var comId = $('#comSelect');
		      var braId = $('#braSelect');
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	comId: comId.val(),
		        	braId: braId.val(),
		        }
		      });
		    }
		  };
		  
		  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
		  
		  $('.demoTable #insert').on('click', function(){
		        	layer.open({
		    	        type: 1	 //此处以iframe举例
		    	        ,title: '增加类型'
		    	        ,area: ['500px', '260px']
		        		,offset: 'auto'
		    	        ,content:$('#addOrUpdate')
		    	        ,end : function() {
		        			$("#operUser").attr({"disabled":false});
		    	        	//弹框关闭的回调函数
		    				$("#addOrUpdate").hide();
		    				exit();
		    			}
		    	    });
		  });
		  
		  $('#sub').on('click', function(){
			  
			 var url="../../matype/addOrUpdate.action"; 
			 var data=$("#myform").serialize();
			 $.post(url,data,function(mes){
				 if(mes==1){
					 exit();
					 $("#addOrUpdate").hide();
					 parent.layer.msg('操作成功', {
					      icon: 1
					  });
					//重载表单
 					active['reload'] ? active['reload'].call(this) : '';
				 }else{
					 parent.layer.msg('操作失败', {
					      icon: 2
					  });
				 }
			 },"json");
		  });

});
function load() {
	 var form = layui.form
	 form.render();
}